<?php
namespace app\admin\controller;
use think\Cookie;
use think\facade\Db;
use think\facade\Env;
use think\helper\Time;
use think\facade\View;
use auth\Auth;
use app\admin\model\AuthAdmin as authAdminModel;
class Index extends Common
{
    public function __construct(){
        parent::__construct();
    }

    public function index(){
		$menus=Auth::leftMenu();
		$this->assign("menus",$menus);
        $count = Db::name('goods')->count();//商品
        $this->assign('count',$count);
		$iframeUrl=isset($menus[0]['children'][0]['url'])?$menus[0]['children'][0]['url']:url("admin/index/console");
		$this->assign("iframeUrl",$iframeUrl);
        $shenhe = Db::name('orders')->where('status', 1)->count();
        $this->assign('shenhe',$shenhe);
        return View::fetch();
    }

    
    public function console(){
        //待回复评论
        $count['user_count'] = Db::name('user')->count();
        $shenhe = Db::name('orders')->where('status', 2)->count();
        $hexiao = Db::name('orders')->where('status', 3)->count();
        $order = Db::name('orders')->where('status', '>',1)->field('sum(total_price) as sum')->find();

        $this->assign('order_money',$order['sum']);
        $this->assign('order_num',$shenhe);
        $this->assign('hexiao',$hexiao);
        $this->assign('count',$count);
		$this->assign("MYSQL_VERSION",$this->_mysql_version());
		$this->assign("os",PHP_OS);
        $xAxis = [];
        $series = [];
        $this->assign('xAxis',$xAxis);
        $this->assign('series',$series);

        return View::fetch();;
    }
	
    /**
     * 获取mysql版本号
     * @author admin
     * @date 2018-04-29
     */	
    private function _mysql_version()
    {
        $version = Db::query("select version() as ver");
        return $version[0]['ver'];
    }	

    public function clear(){
		try{
			$R = BASE_PATH."../runtime/cache/";
			if (_deleteDir($R)) {
				return ['code'=>0,'msg'=>'清除缓存成功'];
			} else {
				return ['code'=>1,'msg'=>'清除缓存失败'];
			}
		} catch (\Exception $e) {
   			 // 这是进行异常捕获
			 return ['code'=>1,'msg'=>'清除异常或者已经没有缓存'];
		}
    }
	
    /**
     * 修改密码
     * @author admin
     * @date 2019-08-08
     */
	public function password(){
            if (request()->isPost()) {
                $data = input('post.');
                $u_pwd = input('post.u_pwd',0,'trim');
				$update=array();
                if ($u_pwd) {
                    $update['u_pwd'] = md5($u_pwd);
                }
				$update['email']=$data['email'];
				$update['tel']=$data['tel'];
				$where['id'] = session(config("auth.USER_AUTH_KEY"));
                authAdminModel::update($update, $where);
                return ['code' => 0, 'msg' => '操作成功!'];
            } else {
                $auth_group = Db::name("auth_group")->select();
                $info = Db::name("auth_admin")->where("id",session(config("auth.USER_AUTH_KEY")))->find();
                unset($info['u_pwd']);
                $this->assign('info', json_encode($info, true));
                $this->assign('authGroup', $auth_group);
                return view();
            }
	}	


    //退出登陆
    public function logout(){
        session(null);
        $url=\think\facade\Route::buildUrl(SYS_PATH."/login/index");
        return ['code' => 0, 'msg' => '退出成功!','url'=>$url];
    }
    
}
